package de.hska.util;

import static de.hska.util.LogUtil.begin;
import static de.hska.util.LogUtil.end;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.sql.SQLException;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbunit.DatabaseUnitException;


public class DeployUtil {
	private static final Log LOG = LogFactory.getLog(DeployUtil.class);
	private static final boolean DEBUG = LOG.isDebugEnabled();
	
	private static String DEPLOY_PATH = System.getenv("JBOSS_HOME") + File.separatorChar +
                                                      "server" + File.separatorChar +
                                                      "default" + File.separatorChar +
                                                      "deploy" + File.separatorChar;

	private static String EAR = "hska.ear";
	private static String TEST_EAR = "testHska.ear";

	private static URL EAR_URL;
	private static URL TEST_EAR_URL;
	private static MBeanServerConnection SERVER;

	static {
		File earFile = new File(DEPLOY_PATH + EAR);
		URI uri = earFile.toURI();
		try {
			EAR_URL = uri.toURL();
		}
		catch (MalformedURLException e) {
			throw new IllegalArgumentException(e);
		}
		
		earFile = new File(DEPLOY_PATH + TEST_EAR);
		uri = earFile.toURI();
		try {
			TEST_EAR_URL = uri.toURL();
		}
		catch (MalformedURLException e) {
			throw new IllegalArgumentException(e);
		}
		
		Context ctx;
		try {
			ctx = new InitialContext();
			SERVER = (MBeanServerConnection) ctx.lookup("jmx/invoker/RMIAdaptor");
			ctx.close();
		}
		catch (NamingException e) {
			throw new IllegalArgumentException(e);
		}
	}
	
	/**
	 */
	public static void main(String[] args) throws InstanceNotFoundException, MalformedObjectNameException, MBeanException, ReflectionException, NullPointerException, IOException, InterruptedException, DatabaseUnitException, SQLException {
		redeploy();
	}
	
	/**
	 */
	public static void redeploy() throws InstanceNotFoundException, MalformedObjectNameException, MBeanException, ReflectionException, NullPointerException, IOException, InterruptedException, DatabaseUnitException, SQLException {
		if (DEBUG) begin(LOG);

		DbUtil.reload();
		
		// Redeploy der zu testenden EAR-Anwendung
		SERVER.invoke(new ObjectName("jboss.system:service=MainDeployer"), "redeploy", new Object[] { EAR_URL }, new String[] { "java.net.URL" });

		Thread.sleep(1000);
		
		// Redeploy der Test-EAR-Anwendung
		SERVER.invoke(new ObjectName("jboss.system:service=MainDeployer"), "redeploy", new Object[] { TEST_EAR_URL }, new String[] { "java.net.URL" });
		
		if (DEBUG) end(LOG);
	}
}
